package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.*;
import org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder;
import org.linlinjava.litemall.db.domain.LitemallType;
import org.linlinjava.litemall.db.domain.LitemallUserEr;
import org.linlinjava.litemall.db.service.LitemallUserErService;
import org.linlinjava.litemall.wx.dto.UserInfo;
import org.linlinjava.litemall.db.service.LitemallTypeService;
import org.linlinjava.litemall.wx.service.UserTokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.linlinjava.litemall.wx.util.WxResponseCode.*;

/**
 * 鉴权服务
 */
@RestController
@RequestMapping("/wx/auth")
@Validated
public class WxAuthErController {
    private final Log logger = LogFactory.getLog(WxAuthErController.class);

    @Autowired
    private LitemallUserErService userErService;

    @Autowired
    private LitemallTypeService typeService;


    /**
     * 账号登录
     *
     * @param body    请求内容，{ username: xxx, password: xxx }
     * @param request 请求对象
     * @return 登录结果
     */
    @PostMapping("loginEr")
    public Object login(@RequestBody String body, HttpServletRequest request) {
        String username = JacksonUtil.parseString(body, "username");
        String password = JacksonUtil.parseString(body, "password");
        if (username == null || password == null) {
            return ResponseUtil.badArgument();
        }

        List<LitemallUserEr> userList = userErService.queryByUsernameEr(username);
        LitemallUserEr user = null;
        if (userList.size() > 1) {
            return ResponseUtil.serious();
        } else if (userList.size() == 0) {
            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号不存在");
        } else {
            user = userList.get(0);
        }
        if (!password .equals(user.getPassword())) {
            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号密码不对");
        }
        List<LitemallType> typeList = typeService.queryByType();
        userErService.updateUserDeleteId(user.getId());
        Integer id = user.getId();
        String userUsername = user.getUsername();
        // userInfo
        UserInfo userInfo = new UserInfo();
        userInfo.setNickName(username);
        // token
        String token = UserTokenManager.generateToken(user.getId());
        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("token", token);
        result.put("userInfo", userInfo);
        result.put("id", id);
        result.put("Username", userUsername);
        result.put("typeList", typeList);
        return ResponseUtil.ok(result);
    }

    @PostMapping("returnMsg")
    public Object createUsers(@RequestBody LitemallUserEr litemallUserErs) {
        Integer id = litemallUserErs.getId();
        LitemallUserEr litemallUserEr = userErService.selectUsernamePwd(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", litemallUserEr.getUsername());
        map.put("password", litemallUserEr.getPassword());
        return ResponseUtil.ok(map);
    }

    @PostMapping("getBalance")
    public Object getBalance(@RequestBody LitemallUserEr litemallUserEr) {
        String username = litemallUserEr.getUsername();
        if (username==null){
            return ResponseUtil.badArgument();
        }
        List<LitemallUserEr> userList = userErService.queryByUsernameEr(username);
        String balance = userList.get(0).getBalance();
        HashMap<String, Object> map = new HashMap<>();
        map.put("balance", balance);
        return ResponseUtil.ok(map);
    }
}
